<?php

/*	Created by Alexander Alexandrov aka SS

			hazg@mail.ru				*/
define('SSLIB_Text', TRUE);

class Text
{
	static function MakeDottedNumber($num, $separator = ".", $step = 3)
	{
		$text = strrev($num);
		$text = chunk_split($text, $step, $separator);
		$text = trim($text, $separator);
		$text = strrev($text);
		return $text;
		//return strrev(trim(chunk_split(strrev($num), $step, $separator), $separator));
	}
	static function CutText($text, $len = 200, $ender = "...")
	{
		
		return mb_substr($text, 0, $len).((strlen($text) > ($len - 1))?$ender:"");
	}
	static function Lower($text)
	{
		return strtolower($text);
	}
	static function Convert($from, $to, $text)
	{
		return iconv($from,$to,$text);
	}
	static function Crypt($text)
	{
		return md5($text);
	}
	static function Safe($text)
	{
	    return str_replace(array('<?', '<?php', '?>'), '', $text);
	}
	function HtmlToTxt($text){
		$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
		               '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
		               '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
		               '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments including CDATA
		);
		return preg_replace($search, '', $text);
    }
	static function Tidy($text, $configPath)
	{
	    $ret  ='';
	    $tempPath = Sslib::SiteRoot('/tmp/').RandomKey();
	    
	    if(function_exists('tidy_parse_string'))
	    {
            file_put_contents($tempPath, $text);
		   
            $tidy = new tidy($tempPath, $configPath);
	        $tidy->CleanRepair();

	        unlink($tempPath);
            $ret = $tidy;
            unset($tidy);
	    }
	    else 
	    {
		    $tidyPath = Sslib::SiteRoot().'private/utils/tidy';
		    $cfgPath = Sslib::SiteRoot().'private/utils/configs/blogs';
		    if(!file_exists($tidyPath))
		        new Err(__T('file not found').' '.$pathPath);
		    file_put_contents($tempPath, $text);
		    exec("$tidyPath -config $cfgPath \"$tempPath\"", $i, $ret);
	        $content = implode("\n",$i);
	        unlink($tempPath);
	        $ret = $content;
	    }
        if(preg_match('#<body.*?>(.*?)</body>#is', $ret, $matchesBody))
        {
            if(preg_match_all('#<style.*?>(.*?)</style>#is', $ret, $matchesStyle, PREG_PATTERN_ORDER))
            {
                $styles = implode($matchesStyle[1]);
                $ret = '<style type="text/css">'.$styles.'</style>'.$matchesBody[1];
            }
            else $ret = $matchesBody[1];
        }
	    return $ret;
	}
}

?>